<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>设置二级密码</title>
    <link rel="stylesheet" href="../assets/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../assets/style/admin.css">
    <style>
        .layui-form-label {
            min-width: 120px;
        }
    </style>
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <script src="../assets/layui/layui.js"></script>
    <script src="../assets/js/common.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <script src="../common/header.js"></script>
    <!-- 内容主体区域 -->
    <div id="LAY_app_body">
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-header">设置二级密码</div>
                        <div class="layui-card-body">
                            <form class="layui-form" action="">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">当前密码</label>
                                    <div class="layui-input-inline">
                                        <input type="password" name="password" lay-verify="password" autocomplete="off"
                                               placeholder="请输入当前密码" class="layui-input" value="">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">二级密码</label>
                                    <div class="layui-input-inline">
                                        <input type="password" name="protect_password" lay-verify="protect_password"
                                               autocomplete="off" placeholder="请输入二级密码" class="layui-input" value=""
                                               id="protect_password">
                                    </div>
                                    <div class="layui-form-mid layui-word-aux">6到16个字符</div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">确认二级密码</label>
                                    <div class="layui-input-inline">
                                        <input type="password" name="protect_password2" lay-verify="protect_password2"
                                               autocomplete="off" placeholder="请再次输入二级密码" class="layui-input" value="">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <div class="layui-input-block">
                                        <a class="layui-btn" lay-submit lay-filter="formSubmit">保存</a>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        layui.use(['form'], function () {
            var $ = layui.$
                , form = layui.form;

            form.verify({
                password: function (value) {
                    if (value.length < 1) {
                        return '请输入当前密码';
                    }
                }
                , protect_password: function (value) {
                    if (value.length < 1) {
                        return '请输入二级密码';
                    } else if (value.length < 6 || value.length > 16) {
                        return '请输入6到16个字符';
                    }
                }
                , protect_password2: function (value) {
                    if (value.length < 1) {
                        return '请再次输入二级密码';
                    }
                    var new_password = $('#protect_password').val();
                    if (value !== new_password) {
                        return '两次输入的二级密码不一致';
                    }
                }
            });

            //监听提交
            form.on('submit(formSubmit)', function (data) {
                data.field.password = hex_md5(data.field.password);
                data.field.protect_password = hex_md5(data.field.protect_password);
                data.field.protect_password2 = hex_md5(data.field.protect_password2);
                layer.msg('提交中', {
                    shadeClose: false
                    , icon: 16
                    , shade: 0.01
                });
                $.ajax({
                    type: "POST",
                    url: "/account/saveProtectPassword",
                    data: data.field,
                    success: function (response) {
                        var dataObj = $.parseJSON(response);
                        if (dataObj.code === 0) {
                            layer.msg(dataObj.msg, {offset: '45vh', icon: 6, time: 2000});
                            setTimeout(function () {
                                location.href = '/account/setProtectPassword';
                            }, 1000);
                        } else {
                            layer.msg(dataObj.msg, {offset: '45vh', icon: 5, time: 2000});
                        }
                    },
                    error: function (request, status, error) {
                        layer.msg('哦噢，网络开小差了', {offset: '45vh', icon: 5, time: 2000});
                    }
                });
                return false;
            });
        });
    </script>
</div>
</body>
</html>
